import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_house_manager/api/home.dart';
import 'package:flutter_house_manager/pages/Home/components/home_list.dart';
import 'package:flutter_house_manager/pages/Home/components/home_nav.dart';
import 'package:flutter_house_manager/utils/TokenManager.dart';
import 'package:flutter_house_manager/utils/toast.dart';

class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  // 3. 存放公共数据列表
  List _announceList = [];

  @override
  void initState() {
    super.initState();
    getAnnounceList();
  }

  /// 1. 获取公共列表
  getAnnounceList() async {
    try {
      // 2. 调用接口获取公共列表
      final data = await getAnnounceListAPI();
      PromptAction.showSuccess('获取公共列表成功');
      setState(() {
        // 4. 赋值给公共数据列表
        _announceList = data;
      });
    } catch (e) {
      PromptAction.showError('获取公共列表成功');
    }
    // final data = await getAnnounceListAPI();
    // showDialog(
    //     context: context,
    //     builder: (context) {
    //       return AlertDialog(
    //         content: Text(data.toString()),
    //       );
    //     });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('首页')),
      body: ListView(scrollDirection: Axis.vertical, children: [
        // 测试token
        testToken(),
        // 测试定时器
        // testTimer(),
        // 导航组件
        const HomeNav(),
        Image.asset('assets/images/banner@2x.jpg'),
        // 列表组件
        HomeList(list: _announceList),
      ]),
    );
  }

  int _count = 60;
  Timer? _timer;

  testTimer() {
    return Row(
      children: [
        ElevatedButton(
          onPressed: () {
            if (_timer != null) {
              return;
            }
            // Timer.periodic: 定时器，里面设置了延迟时间 每秒执行一次
            _timer = Timer.periodic(const Duration(seconds: 1), (timer) {
              setState(() {
                _count--;
              });
            });
          },
          child: const Text('开始'),
        ),
        Text('$_count s'),
        ElevatedButton(
          onPressed: () {
            // 取消定时器
            _timer?.cancel();
            _timer = null;
          },
          child: const Text('结束'),
        )
      ],
    );
  }

  testToken() {
    return Row(
      children: [
        ElevatedButton(
            onPressed: () {
              tokenManager.setToken('123');
            },
            child: const Text('设置token')),
        ElevatedButton(
            onPressed: () async {
              final token = tokenManager.getToken();
              PromptAction.showSuccess('token: $token');
            },
            child: const Text('获取token')),
        ElevatedButton(
            onPressed: () async {
              await tokenManager.removeToken();
              PromptAction.showSuccess('删除token成功');
            },
            child: const Text('删除token')),
      ],
    );
  }
}
